package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.InChart;
import com.ruoyi.common.core.domain.entity.LeftoverIn;
import com.ruoyi.common.core.domain.entity.LeftoverInDetail;
import com.ruoyi.common.core.domain.entity.Series;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.LeftoverInMapper;
import com.ruoyi.system.service.ILeftoverInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class LeftoverInServiceImpl implements ILeftoverInService {

    @Autowired
    private LeftoverInMapper leftoverInMapper;

    @Override
//    @DataSource(value = DataSourceType.SLAVE)
    public List<LeftoverInDetail> selectLeftoverInList(LeftoverInDetail leftoverInDetail) {
        return leftoverInMapper.selectLeftoverInList(leftoverInDetail);
    }

    @Override
    public LeftoverInDetail selectLeftoverByTableId(String inId) {
        return leftoverInMapper.selectLeftoverByTableId(inId);
    }

    @Override
    @Transactional
    public int updateLeftoverIn(LeftoverInDetail leftoverInDetail) {
        return leftoverInMapper.updateLeftoverIn(leftoverInDetail);
    }

    @Override
    public int insertLeftoverIn(LeftoverInDetail[] leftoverInDetails, String manage, String receive, String floor) {

        String inId = UUID.randomUUID().toString();
        LeftoverIn leftoverIn = new LeftoverIn();
        leftoverIn.setInId(inId);
        leftoverIn.setManage(manage);
        leftoverIn.setReceive(receive);

        leftoverInMapper.insertLeftoverIn(leftoverIn);

        for (int i = 0; i < leftoverInDetails.length; i++) {
            leftoverInDetails[i].setTableId(UUID.randomUUID().toString());
            leftoverInDetails[i].setInId(inId);
            leftoverInDetails[i].setFloor(floor);
            leftoverInMapper.insertLeftoverInDetail(leftoverInDetails[i]);
        }
        return leftoverInDetails.length;
    }

    @Override
    public int updateLeftoverInState(LeftoverInDetail leftoverInDetail) {
        return leftoverInMapper.updateLeftoverInState(leftoverInDetail);
    }

    @Override
    public void updateLeftoverNum(LeftoverInDetail leftoverInDetail) {
        String tableId = leftoverInDetail.getTableId();
        List<LeftoverInDetail> leftoverInDetails = leftoverInMapper.selectLeftoverInDetailList(tableId);
        for (LeftoverInDetail leftover : leftoverInDetails) {
            leftover.setTableId(UUID.randomUUID().toString());
            leftover.setInNum(leftover.getInNum() * (1 - leftover.getProportion()));
            leftoverInMapper.updateLeftoverNum(leftover);
        }
    }

    @Override
    public void updateLeftoverInDetail(LeftoverInDetail leftoverInDetail) {
        leftoverInMapper.updateLeftoverInDetail(leftoverInDetail);
    }

    @Override
    public List<InChart> selectLeftoverInChart(LeftoverInDetail leftoverInDetail) {
        return leftoverInMapper.selectLeftoverInChart(leftoverInDetail);
    }

    @Override
    public List<String> selectMaterial() {
        return leftoverInMapper.selectMaterial();
    }

    @Override
    public int[] selectData(String material) {
        return leftoverInMapper.selectData(material);
    }

    @Override
    public List<Series> handelData() {
        List<String> materials = selectMaterial();
        List<Series> seriesList = new ArrayList<>();
        for (String material : materials) {
            Series series = new Series();
            series.setName(material);
            series.setType("bar");
            series.setBarGap(0);
            int[] data;
            data = selectData(material);
            series.setData(data);
            seriesList.add(series);
        }
        return seriesList;
    }

    @Override
    public int[] selectData1(String material) {
        return leftoverInMapper.selectData1(material);
    }

    @Override
    public List<Series> handelData1() {
        List<String> materials = selectMaterial();
        List<Series> seriesList = new ArrayList<>();
        for (String material : materials) {
            Series series = new Series();
            series.setName(material);
            series.setType("bar");
            series.setBarGap(0);
            int[] data;
            data = selectData1(material);
            series.setData(data);
            seriesList.add(series);
        }
        return seriesList;
    }
}
